<?php
/**
 * 处理类
 * User: 91336
 * Date: 13-7-5
 * Time: 下午1:40
 */

class extclass{
    static $my_class = false;
    static $class_name;

    /**
     * 设置需要处理的类名
     * @param $class_name
     */
    public static function _setClass($class_name){
        self::$class_name = $class_name;
    }

    /**
     * 创建类实例
     * @return bool|ReflectionClass
     */
    public static function instance(){
        if(self::$my_class instanceof ReflectionClass) return self::$my_class;
        if(class_exists(self::$class_name))
            self::$my_class = new ReflectionClass(self::$class_name);
        if(self::$my_class->isAbstract())
            self::$my_class = false;
        return self::$my_class;
    }

    /**
     * 获取类方法
     */
    public static function getMethods(){
        if(!self::instance()) return array();
        $methods = self::instance()->getMethods();
        $result = array();
        foreach($methods as $item){
            if($item->class != self::$class_name)
                continue;
            $item = (array)$item;
            $result[] = array_merge($item, self::getDocElement($item['name']));
        }
        return $result;
    }

    /**
     * 获取方法注释
     * @param $method
     * @return array
     */
    public static function getDocElement($method){
        if(!self::instance()) return array();
        $doc = self::instance()->getMethod($method)->getDocComment();
        $doc = preg_replace('/\r|\n|\/|\*/', '', $doc);
        $doc = explode('@', $doc);
        $element = array();
        foreach($doc as $item){
            $item = trim($item);
            if(empty($item)) continue;
            $item = explode(' ', $item);
            $element[$item[0]] = $item[1];
        }
        return $element;
    }
}